package com.globalwave.common.virtualmodule
{
	import com.globalwave.common.C;
	import com.globalwave.common.CodeComboBox;
	import com.globalwave.common.DateField;
	import com.globalwave.common.FilterComboBox;
	import com.globalwave.common.RadioButtons;
	import com.globalwave.common.SearchTextInput;
	import com.globalwave.common.validator.SmartValidator;
	
	import mx.collections.ArrayCollection;
	import mx.containers.FormItem;
	import mx.controls.ComboBox;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	
	public class VirtualFormItem extends FormItem
	{
		public var input:UIComponent ;
		public var field:XML ;
		public var validator:SmartValidator ;
		public var valueProperty:String ;
		
		public function VirtualFormItem(
			field:XML, 
			validators:ArrayCollection=null, 
			idNames:ArrayCollection=null,
			defaultObject:Object=null)
		{
			super();
			//this.setStyle("labelWidth", 180) ;
			this.percentWidth = 100 ;
			
			if (idNames != null && C.INTEGER_TRUE == field.is_primary_key) {
				idNames.addItem(field.code_);
			}
			
			if (C.INTEGER_FALSE == field.is_visible) {
			    return ;
			}
			
			this.required = (C.INTEGER_FALSE == field.is_nullable) ;
			this.label = field.name_ ;
			
			if("CodeComboBox" == field.input_type) {
				var ccb:CodeComboBox = new CodeComboBox() ;
				input = ccb ;
				var codeValues:Array = (field.input_params + "").split(":") ;
				ccb.valueField = codeValues[1];
				ccb.labelField = codeValues[2];
				ccb.code = codeValues[0];
				valueProperty = "selectedValue" ;
			} else if("ComboBox" == field.input_type) {
				var cb:FilterComboBox = new FilterComboBox() ;
				input = cb ;
				cb.dataProvider = field.options ;
				valueProperty = "selectedValue" ;
			} else if("Radio" == field.input_type) {
				var vb:RadioButtons = new RadioButtons() ;
				input = vb ;
				vb.dataProvider = field.options.object ;
				valueProperty = "selectedValue" ;
			} else if("Search" == field.input_type) {
				var sti:SearchTextInput = new SearchTextInput() ;
				input = sti ;
				var values:Array = (field.input_params + "").split(":") ;
				sti.code = values[0];
				//sti.url = values[1];
				sti.valueField = values[1];
				sti.displayField = values[2];
				sti.minLength = values[3];
				valueProperty = "value" ;
			} else if ("DateField" == field.input_type) {
				input = new DateField()
				//input.percentWidth = 100 ;
				valueProperty = "text" ;
			} else {
			    input = new TextInput() ;
				input.percentWidth = 100 ;
				valueProperty = "text" ;
			}
			this.addChild(input);
			input.name = field.code_ ;
			this.field = field ;
			
			if(defaultObject != null) {
				defaultObject[input.name] = (C.INTEGER_TRUE == field.is_primary_key)?"":field.default_ ;
			}
			
			if (validators != null) {
				validator = new SmartValidator() ;
				validators.addItem(validator);
				
				validator.source = input ;
				
				validator.property = valueProperty ;
				validator.required = (C.INTEGER_FALSE == field.is_nullable) ;
				/*if("ComboBox" != field.input_type
					    && "Radio" != field.input_type
				        && "Search" != field.input_type) {*/
				if (validator.property != "selectedValue" ){
				    validator.rule = field.rule_1 ;
				}
				validator.isNumber = (C.TYPE_INTEGER == field.type_ || C.TYPE_BIGINT == field.type_ || C.TYPE_DOUBLE == field.type_) ;
				validator.maxValue = (field.max_value == "")?null:field.max_value ;
				validator.minValue = (field.min_value == "")?null:field.min_value ;
				validator.maxLength = (field.max_length == "")?null:field.max_length ;
				validator.minLength = (field.min_length == "")?null:field.min_length ;
			}
		}
		
		
		public function set value(value:String):void {
			input[valueProperty] = value ;
			/*
			if("CodeComboBox" == field.input_type) {
				input["selectedValue"] = value ;
			} else if("ComboBox" == field.input_type) {
				(input as FilterComboBox).selectedValue = value ;
			} else if("Radio" == field.input_type) {
				(input as RadioButtons).selectedValue = value ;
			} else if("Search" == field.input_type) {
				(input as SearchTextInput).value = value;
			} else {
				(input as TextInput).text = value ;
			}	*/	
		}
		
		public function get value():String {
			/*
			if("CodeComboBox" == field.input_type) {
				return input["selectedValue"] ;
			} else if("ComboBox" == field.input_type) {
				return (input as FilterComboBox).selectedValue ;
			} else if("Radio" == field.input_type) {
				return (input as RadioButtons).selectedValue ;
			} else if("Search" == field.input_type) {
				return (input as SearchTextInput).value ;
			} else {
				return (input as TextInput).text ;
			}*/
			return input[valueProperty] ;
		}
	}
}